<?php
namespace app\system\controller;

use app\common\model\FormFieldModel;
use app\common\model\FormModel;
use think\Db;
use think\Request;

class FormController extends BaseController
{

    private $formModel;
    private $formFieldModel;

    /**
     * 初始化
     */
    public function __construct(Request $request)
    {
        parent::__construct($request);
        $this->formModel      = new FormModel();
        $this->formFieldModel = new FormFieldModel();
    }

    /**
     * 首页
     * @return mixed
     */
    public function index()
    {
        $map        = [];
        $query      = [];
        $searchKey  = $this->request->param('searchKey');
        $searchText = $this->request->param('searchText');
        if (!empty($searchKey) && !empty($searchText)) {
            $map[$searchKey]     = ['like', '%' . $searchText . '%'];
            $query['searchKey']  = $searchKey;
            $query['searchText'] = $searchText;
        }
        $list = $this->formModel->where($map)->order('form_sort asc')->paginate(10, false, [
            'query' => $query,
        ]);
        $this->assign('list', $list);
        $this->assign('query', $query);
        return $this->fetch();
    }

    /**
     * 添加表单
     * @return mixed
     */
    public function add()
    {
        if ($this->request->isAjax()) {
            $data   = $this->request->param();
            $result = $this->validate($data, 'FormValidate.add');
            if (true !== $result) {
                $this->error($result);
            }
            if ($this->formModel->addData($data)) {
                $this->formModel->createTable($data['table_name']);
                $this->success('添加成功！', url('index'));
            } else {
                $this->error($this->formModel->getError());
            }
        }
        return $this->fetch();
    }

    /**
     * 修改表单
     * @return mixed
     */
    public function edit()
    {
        if ($this->request->isAjax()) {
            $data   = $this->request->param();
            $result = $this->validate($data, 'FormValidate.edit');
            if (true !== $result) {
                $this->error($result);
            }
            if ($this->formModel->editData($data)) {
                $this->success('修改成功！', url('index'));
            } else {
                $this->error($this->formModel->getError());
            }
        }
        $formId = $this->request->param('form_id');
        $info   = $this->formModel->get($formId);
        $this->assign('info', $info);
        return $this->fetch();
    }

    /**
     * 设置状态
     */
    public function setStatus()
    {
        $formId     = $this->request->param('form_id');
        $formStatus = $this->formModel->where('form_id', $formId)->value('form_status');
        $formStatus = $formStatus == 1 ? '0' : '1';
        $this->formModel->where('form_id', $formId)->update(['form_status' => $formStatus]);
        $this->success('设置成功！', url('index'));
    }

    /**
     * 删除表单
     */
    public function del()
    {
        $formId = $this->request->param('form_id');
        $result = $this->formModel->delData($formId);
        if ($result == true) {
            $this->success('删除成功！');
        } else {
            $this->error($this->formModel->getError());
        }
    }

    /**
     * 表单预览
     */
    public function createHtml()
    {
        $formId    = $this->request->param('form_id');
        $formInfo  = $this->formModel->get($formId);
        $fieldList = $this->formFieldModel->order('field_sort asc')->where('form_id', $formId)->select();
        foreach ($fieldList as $k => $v) {
            $function              = '_' . $v['field_type'];
            $fieldList[$k]['html'] = $this->formFieldModel->$function($v);
        }
        $this->assign('formInfo', $formInfo);
        $this->assign('fieldList', $fieldList);
        return $this->fetch();
    }

    /**
     * 表单反馈
     */
    public function feedList()
    {
        $formId    = $this->request->param('form_id');
        $tableInfo = $this->formModel->where('form_id', $formId)->find();
        $this->assign('tableInfo', $tableInfo);//表信息
        $dataList  = Db::table('form_' . $tableInfo['table_name'])->order('create_time desc')->paginate(10, false);
        $fieldList = $this->formFieldModel->where('form_id', $formId)->order('field_sort asc')->select();
        $this->assign('dataList', $dataList);//留言条合集
        $this->assign('fieldList', $fieldList);//字段合集
        return $this->fetch();
    }

    /**
     * @return mixed
     * @throws \think\Exception
     * 编辑回复信息
     */
    public function reply()
    {
        if ($this->request->isAjax()) {
            $data = $this->request->param();
            if (empty($data['reply_note'])) {
                $this->error('回复内容不能为空！');
            }
            $tableName = $this->formModel->where('form_id', $data['form_id'])->value('table_name');
            Db::table('form_' . $tableName)->where('id', $data['id'])->update(['reply_note' => $data['reply_note'], 'reply_time' => time()]);
            $this->success('回复成功', url('feedList', ['form_id' => $data['form_id']]));
        }
        $formId    = $this->request->param('form_id');
        $id        = $this->request->param('id');
        $tableInfo = $this->formModel->where('form_id', $formId)->find();
        $info      = Db::table('form_' . $tableInfo['table_name'])->where('id', $id)->find();
        $this->assign('tableInfo', $tableInfo);//表信息
        $this->assign('info', $info);//留言条信息
        return $this->fetch();
    }

    /**
     * 删除反馈
     */
    public function feedInfoDel()
    {
        $id        = $this->request->param('id');
        $formId    = $this->request->param('form_id/d');
        $tableInfo = $this->formModel->where('form_id', $formId)->find();
        Db::table('form_' . $tableInfo['table_name'])->where('id', $id)->delete();
        $this->success('删除成功', url('feedList', ['form_id' => $formId]));
    }
}
